package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.InspectionFocus;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther: bin.liu
 * @Date: 2019/1/11 14:19
 * @Description: 巡检工单关注dao
 */
@Repository("inspectionFocusDao")
public class InspectionFocusDao implements CrudDao<InspectionFocus> {

    private final static Logger logger = LoggerFactory.getLogger(InspectionFocusDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("f.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String systemUserId = queryHash.get("systemUserId");
        if (StringUtils.isNotBlank(systemUserId)) {
            conditionHash.put("f.systemUser.id = ?{paramIndex}", StringConverters.null2Long(systemUserId));
        }

        String enterpriseId = queryHash.get("enterpriseId");
        if (StringUtils.isNotBlank(enterpriseId)) {
            conditionHash.put("f.enterprise.id = ?{paramIndex}", StringConverters.null2Long(enterpriseId));
        }

        String inspectionOrderId = queryHash.get("inspectionOrderId");
        if (StringUtils.isNotBlank(inspectionOrderId)) {
            conditionHash.put("f.inspectionOrder.id = ?{paramIndex}", StringConverters.null2Long(inspectionOrderId));
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(f) from InspectionFocus f ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<InspectionFocus> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select f from InspectionFocus f ", "order by f.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<InspectionFocus> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, InspectionFocus.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public InspectionFocus findModel(Long id) {
        return entityManager.find(InspectionFocus.class, id);
    }

    @Override
    public Integer add(InspectionFocus model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        return 1;
    }

    @Override
    public Integer update(InspectionFocus model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        InspectionFocus inspectionFocus = entityManager.find(InspectionFocus.class, id);
        entityManager.remove(inspectionFocus);
        return 1;
    }

    @Override
    public Integer deleteLogic(InspectionFocus model) {
        model.setDeleteStatus(true);
        return 1;
    }

}
